//
//  HSMessageViewModal.swift
//  SchoolDemo
//
//  Created by rimi on 16/10/11.
//  Copyright © 2016年 YQ. All rights reserved.
//

import UIKit

class HSMessageViewModal {
    
    //单一消息模型
    var message: HSMessageModal {
        //在点击回复或者点赞的时候，重新计算高度和富文本转换
        didSet {
            //这里处理属性计算和高度计算
            likeNameAttribute = likeAttributeWith(str: message.likelist ?? "")
            //计算高度
            setupBgViewH()
        }
    }
    //点赞人名的富文本
    var likeNameAttribute:NSAttributedString = NSAttributedString()
    //回复内容的富文本数组
    var replayAttributeArray:[NSAttributedString] = []
    //给外界使用的高度
    var replayViewH:CGFloat = 0
    //计算整个cell的高度
    let cellHeight: CGFloat = 0
    
    
    //因为当前类型没有继承任何父类，所以不存在重写父类方法
    //因此不需要override
    init(modal: HSMessageModal) {
        message = modal
        //这里处理属性计算和高度计算
        likeNameAttribute = likeAttributeWith(str: message.likelist ?? "")
        //计算高度
        setupBgViewH()
    }
    
    
    //计算回复视图高度和设置回复类内容的富文本
    fileprivate func setupBgViewH() {
        //高度 = 点赞Label的top + 点赞内容高度 + 间距 + 线高 + (间距 + 回复L的高) * 数量 + 底部间距
        
        //设置默认最大范围
        let maxSize = CGSize(width: repalyViewW, height: CGFloat(MAXFLOAT))
        //创建一个总高度的变量
        var viewH:CGFloat = 0
        //第一个
        let replayNum = message.replaylist?.count ?? 0
        if message.likenum > 0 || replayNum > 0 {
            
            viewH += 12 + 5
            
        }
        if message.likenum > 0 {
            //计算点赞内容的高度
            let likeStrSize = likeNameAttribute.boundingRect(with: maxSize, options: NSStringDrawingOptions.usesLineFragmentOrigin, context: nil).size.height
            //+ 点赞内容高度 + 间距 + 线高
            viewH += (likeStrSize + 5 + 1)
            
        }
        
        //计算回复类容高度
        if let replayList = message.replaylist {
            //创建一个可变数组
            var attrArrayM = [NSAttributedString]()
            for modal in replayList {
                //获得回复数据模型
                let replayM = modal as! HSReplaylistModal
                
                //先将字符串和名称转换为富文本
                let attrName = NSMutableAttributedString(string: (replayM.int_name ?? "")+":", attributes: [NSForegroundColorAttributeName:UIColor().colorWithHex(0x00a0e9),NSFontAttributeName:UIFont.systemFont(ofSize: 13)])
                let attrContent = NSMutableAttributedString(string: replayM.int_content ?? "", attributes: [NSForegroundColorAttributeName:UIColor().colorWithHex(0x808080),NSFontAttributeName:UIFont.systemFont(ofSize: 13)])
                //拼接
                attrName.append(attrContent)
                attrArrayM.append(attrName)
                //计算高度
                let replayStrH = attrName.boundingRect(with: maxSize, options: .usesLineFragmentOrigin, context: nil).size.height
                //+ (间距 + 回复L的高) * 数量
                viewH += (5 + replayStrH)
            }
            //赋值给外界
            replayAttributeArray = attrArrayM
            
            
        }
        //赋值给外界
        replayViewH = viewH
    }
    
    
    //计算属性和高度
    fileprivate func likeAttributeWith(str: String) -> NSAttributedString {
        
        if message.likenum == 0 {
            return NSAttributedString()
            
        }
        
        let attach = NSTextAttachment()
        attach.image = UIImage(named: "notifi_support_press")
        attach.bounds = CGRect(x: 0, y: -2, width: 13, height: 13)
        let imageStr = NSAttributedString(attachment: attach)
        //将点赞字符串转换为富文本
        let likeStrAttribute = NSAttributedString(string: " :" + str,attributes:[NSFontAttributeName:UIFont.systemFont(ofSize: 13)])
        //拼接
        let attributeM = NSMutableAttributedString(attributedString: imageStr)
        attributeM.append(likeStrAttribute)
        
        return attributeM
    }
    
    
}
